package com.magicsoft.share.ui.share;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.ArticleListAdapter;
import com.magicsoft.share.base.BaseFragment;
import com.magicsoft.share.bean.my.ArticleList;
import com.magicsoft.share.bean.share.Special;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.presenter.share.ArticleListPresenter;
import com.magicsoft.share.ui.main.H5Activity;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.utils.GlideImageLoader;
import com.magicsoft.share.view.share.IArticleListView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static android.app.Activity.RESULT_OK;

/**
 * Created by lyf on 2017/10/26/026.
 */

public class HotSpotArticleFragment extends BaseFragment<IArticleListView, ArticleListPresenter> implements IArticleListView {

    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;


    private ArticleListAdapter articleListAdapter;

    private View topView;
    private Banner banner;
    private int pageNum = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;
    private int mType;
    private List<com.magicsoft.share.bean.share.Banner> mBanners;

    public HotSpotArticleFragment() {
        // Required empty public constructor
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_article_list;
    }

    @Override
    public ArticleListPresenter initPresenter() {
        return new ArticleListPresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));


        recyclerView.useDefaultLoadMore();
        //   recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                isRefresh = false;
                pageNum++;
                requestData();
            }
        });


        topView = LayoutInflater.from(getContext()).inflate(R.layout.top_banner, recyclerView, false);
        banner = ((Banner) topView.findViewById(R.id.banner));

        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合

        articleListAdapter = new ArticleListAdapter(R.layout.item_article);
        articleListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                ArticleList.ListBean item = articleListAdapter.getItem(position);
                Intent intent = new Intent(getActivity(), ArticleActivity.class);
                intent.putExtra("aticle", item);
                intent.putExtra("articleId", item.getArticleId());
                Log.e("ffff",item.getArticleId()+"");
                startActivity(intent);
            }
        });
        articleListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_head) {
                    Intent intent = new Intent(getContext(), HomePageActivity.class);
                    intent.putExtra("appUserId", articleListAdapter.getItem(position).getAppUserId());
                    startActivity(intent);
                }
            }
        });

        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg2), 0, DensityUtils.dp2px(getContext(), 10), BaseQuickAdapter.HEADER_VIEW));
        articleListAdapter.addHeaderView(topView);

        recyclerView.setAdapter(articleListAdapter);

    }

    @Override
    protected void requestData() {
        mPresenter.bannerList();
        mPresenter.articleList(null, pageNum, pageSize, null, null, 1);
    }


    public void refreshData() {

        isRefresh = true;
        pageNum = 1;
        mPresenter.bannerList();
        mPresenter.articleList(null, pageNum, pageSize, null, null, 1);
    }

    @Override
    public void bannerListSuccess(List<com.magicsoft.share.bean.share.Banner> banners) {
        mBanners = banners;
        ArrayList<String> images = new ArrayList<>();
        for (com.magicsoft.share.bean.share.Banner ban : banners) {
            images.add(ban.getImg());
        }
        banner.setImages(images);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        });

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {


                com.magicsoft.share.bean.share.Banner ban = mBanners.get(position);
                if (ban.getContentType() == 2) {
                    Intent intent = new Intent(getContext(), H5Activity.class);
                    intent.putExtra("urlStr", ban.getLink());
                    intent.putExtra("titleStr", ban.getTitle());
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getContext(), ArticleActivity.class);
                    intent.putExtra("articleId", ban.getRelationId());
                    Log.e("articleIdfgg",ban.getRelationId()+"");
                    startActivity(intent);
                }
            }
        });


        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    @Override
    public void ArticleListSuccess(ArticleList data) {

        if (isRefresh) {
            articleListAdapter.setNewData(data.getList());
        } else {
            articleListAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }

    }

    @Override
    public void ArticleListFail() {

        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }

    @Override
    public void specialListSuccess(List<Special> specials) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {

            Log.d("lyf", "onActivityResult: ");
            if (requestCode == Constant.REFRESH_UI) {
                isRefresh = true;
                requestData();
            }
        }
    }

    public void setType(int type) {
        mType = type;

    }
}
